package controller;

import java.io.File;
import java.util.HashSet;
import java.util.Set;

import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;

import main.model.Project;
import main.model.persistence.DiagramXmlReader;
import view.StartView;
import controller.editor.EditorController;

public class StartViewController extends Controller{

	public void handleButtonCrear(){
		final JFileChooser fc = new JFileChooser();
		int returnVal = fc.showSaveDialog(StartView.getInstance().getFrame());

		if (returnVal == JFileChooser.APPROVE_OPTION) {
			File file = fc.getSelectedFile();
			String path = file.getAbsolutePath();
			file.mkdir();
			File datos = new File(path + "//Datos");
			datos.mkdir();
			EditorController controller = new EditorController(null);
			controller.createProjectWithCurrentName(path);
			StartView.getInstance().hide();
			controller.displayView();
		}
	}

	public void handleButtonAbrir(){
		final JFileChooser fc = new JFileChooser();
		fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		fc.setAcceptAllFileFilterUsed(false);
		fc.addChoosableFileFilter (new FileFilter() {
			@Override
			public String getDescription() {
				return "Solo directorios";
			}
			
			@Override
			public boolean accept(File f) {
				if (f.isDirectory()) {
					return true;
				}
				return false;
			}
		});
		int returnVal = fc.showOpenDialog(StartView.getInstance().getFrame());
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			File file = fc.getSelectedFile();
			File[] listFiles = file.listFiles();
			Boolean isValidProject = Boolean.TRUE;
			Set<String> fileNames = new HashSet<String>();
			String principal = new String();
			Integer i = 0;
			for(i = 0; ((i < listFiles.length) && isValidProject); i++){
				File localFile = listFiles[i];
				isValidProject = evaluatePath(localFile.getName());
				if (isValidProject && !listFiles[i].getName().equals("Datos") && !listFiles[i].getName().endsWith(".jpg") &&
						!listFiles[i].getName().matches("Principal-(?:comp|rep)\\.xml")){
					fileNames.add(file.getPath() +"/"+ listFiles[i].getName().split("-")[0]);
				}
				if (listFiles[i].getName().matches("Principal-(?:comp|rep)\\.xml")){
					principal = file.getPath() +"/"+ listFiles[i].getName().split("-")[0];
				}
			}

			if (!isValidProject){
				JDialog dialogo = new JDialog(); 
				JOptionPane.showMessageDialog(dialogo,"No se puede abrir este archivo", "Notificacion",
		                    JOptionPane.INFORMATION_MESSAGE);
				
			} else {
				Project project = new Project(fc.getSelectedFile().getPath());
				EditorController controller = new EditorController(project);
				DiagramXmlReader reader = new DiagramXmlReader(controller);
				reader.loadProject(principal, fileNames);
				StartView.getInstance().hide();
				reader.getController().displayView();
			}
		}
	} 

	private Boolean evaluatePath(String name) {
		Boolean result = name.matches("(?si)(?:datos|[^-]*-comp\\.xml|[^-]*-rep\\.xml|.*\\.jpg)");
		return result;
	}

	@Override
	public void displayView() {
		if (StartView.getInstance().getController() == null){
			StartView.getInstance().setController(this);
		}
		StartView.getInstance().show();
	}
}
